package com.zscat.mallplus.dms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.IDmsOrderService;
import com.zscat.mallplus.qsg.QsgDevice;
import com.zscat.mallplus.qsg.service.IDeviceService;
import com.zscat.mallplus.sys.entity.SysUser;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.util.UserUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@Api(tags = "DmsOrderController", description = "驾校订单管理")
@RequestMapping("/admin/dms/order")
public class DmsOrderController {

    @Resource
    private IDmsOrderService dmsOrderService;
    @Resource
    private IDeviceService deviceService;
    @Resource
    private IUmsMemberService memberService;

    @SysLog(MODULE = "dms", REMARK = "根据条件查询所有驾校订单列表")
    @ApiOperation("根据条件查询所有驾校订单列表")
    @GetMapping(value = "/list")
    public Object getOrderByPage(DmsOrder entity,
                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "50") Integer pageSize
    ) {
        SysUser user = UserUtils.getCurrentMember();
        QueryWrapper qw = new QueryWrapper();
        qw.eq("store_id",user.getQsgStoreId());
        if (StringUtils.isNotBlank(entity.getOrderSn())){
            qw.eq("order_sn",entity.getOrderSn());
        }
        if (StringUtils.isNotBlank(entity.getMealName())){
            qw.eq("meal_name",entity.getMealName());
        }
        if (null != entity.getPrice()){
            qw.eq("price",entity.getPrice());
        }
        if (StringUtils.isNotBlank(entity.getStartDate())){
            qw.ge("create_datetime",entity.getStartDate());
        }
        if (StringUtils.isNotBlank(entity.getEndDate())){
            qw.le("create_datetime",entity.getEndDate());
        }
        if (null!=entity.getOrderStatus()){
            qw.eq("order_status",entity.getOrderStatus());
        }else{
            //默认查询有效订单，不查询未付款和已关闭的订单
            qw.notIn("order_status",12,1,15);
        }
        if(null != entity.getOrderType()){
            if(0 == entity.getOrderType().intValue()){
                qw.eq("integral",0);
            }else if(1 == entity.getOrderType().intValue()){
                qw.gt("integral",0);
                qw.isNull("cert_id");
            }else if(2 == entity.getOrderType().intValue()){
                qw.gt("integral",0);
                qw.isNotNull("cert_id");
            }
        }
        qw.orderByDesc("create_datetime");
        try {
            IPage<DmsOrder> page = dmsOrderService.page(new Page<>(pageNum, pageSize), qw);
            for (DmsOrder order : page.getRecords()){
                Long deviceId = order.getDeviceId();
                QsgDevice device = deviceService.getById(deviceId);
                if(null != device){
                    order.setDevice(device);
                    order.setMachineName(device.getMachineName());
                }
                UmsMember member = memberService.getById(order.getMemberId());
                if(null != member){
                    order.setPhone(member.getPhone());
                }
                setOrderStatusName(order);
            }
            return new CommonResult().success(page);
        } catch (Exception e) {
            log.error("根据条件查询所有驾校订单s列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }

    private static void setOrderStatusName(DmsOrder order){
        if (order.getOrderStatus()==2){
            order.setOrderStatusName("已付款");
        }else if (order.getOrderStatus()==7){
            order.setOrderStatusName("已完成");
        }else if (order.getOrderStatus()==20){
            order.setOrderStatusName("待取消");
        }else if (order.getOrderStatus()==14){
            order.setOrderStatusName("已退款");
        }else if (order.getOrderStatus()==15){
            order.setOrderStatusName("已关闭");
        }
        else if (order.getOrderStatus()==1||order.getOrderStatus()==12){
            order.setOrderStatusName("未付款");
        }
    }

    @SysLog(MODULE = "dms", REMARK = "删除驾校订单表")
    @ApiOperation("删除驾校订单表")
    @GetMapping(value = "/delete")
    public Object deleteOrder(Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("驾校订单表id");
            }
            if (dmsOrderService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("删除驾校订单表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "dms", REMARK = "给驾校订单表分配驾校订单表")
    @ApiOperation("查询驾校订单表明细")
    @GetMapping(value = "/{id}")
    public Object getOrderById(@ApiParam("驾校订单表id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("驾校订单表id");
            }
            DmsOrder order = dmsOrderService.getById(id);
            return new CommonResult().success(order);
        } catch (Exception e) {
            log.error("查询驾校订单表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }

    }
}
